package com.dries.portal.module.friends.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dries.common.api.CommonResult;
import com.dries.common.exception.ApiException;
import com.dries.portal.module.friends.entity.UserAttention;
import com.dries.portal.module.friends.enums.AttentionType;
import com.dries.portal.module.friends.form.UserAttrForm;
import com.dries.portal.module.friends.service.IUserAttentionService;
import com.dries.portal.module.friends.vo.UserAttentionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * 用户关注表
 *
 * @author dries
 * @email samphsanie@gmail.com
 * @date 2020-08-17 09:59:44
 */
@RestController
@Api(tags = "用户关注管理", value = "UserAttentionController")
@RequestMapping("friends/attention")
public class UserAttentionController {

    @Resource
    private IUserAttentionService userAttentionService;

    /**
     * 我的关注列表
     */
    @ApiOperation(value = "用户关注列表")
    @GetMapping("/list/{userId}")
    public CommonResult list(@PathVariable String userId){

        List<UserAttentionVo> users = userAttentionService.findMyAttentionListById(userId);
        
        return CommonResult.success(users);
    }

    /**
     * 我的被关注列表
     */
    @ApiOperation(value = "用户被关注列表")
    @GetMapping("/{userId}")
    public CommonResult attentions(@PathVariable String userId){

        List<UserAttentionVo> users = userAttentionService.findAttentionListById(userId);

        return CommonResult.success(users);
    }

    /**
     * 是否被关注
     */
    @ApiOperation(value = "用户是否被关注")
    @PostMapping("/check/to")
    public CommonResult isAttention(@RequestBody UserAttrForm form){

        UserAttention attention = userAttentionService.getOne(new QueryWrapper<UserAttention>().lambda()
                .eq(UserAttention::getUserId, form.getUserId())
                .eq(UserAttention::getAttentionId, form.getAttentionId())
                .eq(UserAttention::getAttStatus, 1)
        );
        if (attention != null){
            return CommonResult.success(1);
        } else {
            return CommonResult.success(0);
        }
    }

    /**
     * 是否互关
     */
    @ApiOperation(value = "用户是否互关")
    @PostMapping("/check")
    public CommonResult friend(@RequestBody UserAttrForm form){

        boolean attr = userAttentionService.checkFriendById(form.getUserId(), form.getAttentionId());

        if (attr){
            return CommonResult.success(1);
        } else {
            return CommonResult.success(0);
        }
    }

    /**
     * 我的关注数
     */
    @ApiOperation(value = "用户关注数量")
    @GetMapping("/count/{userId}")
    public CommonResult attrCount(@PathVariable String userId){

        int count = userAttentionService.count(new QueryWrapper<UserAttention>()
                .lambda().eq(UserAttention::getUserId, userId).eq(UserAttention::getAttStatus, 1)
        );

        return CommonResult.success(count);
    }

    /**
     * 我的被关注数
     */
    @ApiOperation(value = "用户被关注数量")
    @GetMapping("/count/attention/{userId}")
    public CommonResult userCount(@PathVariable String userId){

        int count = userAttentionService.count(new QueryWrapper<UserAttention>()
                .lambda().eq(UserAttention::getAttentionId, userId).eq(UserAttention::getAttStatus, 1)
        );

        return CommonResult.success(count);
    }

    /**
     * 关注或取消关注用户
     */
    @ApiOperation("关注或取消关注用户")
    @PostMapping("/save")
    public CommonResult save(@Valid @RequestBody UserAttention userAttention){

        userAttentionService.attentionByUserId(userAttention);

        if (Objects.equals(userAttention.getAttStatus(), AttentionType.ATTENTION.getCode())) {
            return CommonResult.success("关注成功");
        } else if (Objects.equals(userAttention.getAttStatus(), AttentionType.NO_ATTENTION.getCode())) {
            return CommonResult.success("取关成功");
        } else {
            throw new ApiException("参数错误");
        }

    }



}
